/**
 * 
 */
package cn.crm.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.impl.CriteriaImpl;
import org.hibernate.loader.criteria.CriteriaLoader;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.crm.bean.User;
import cn.crm.bean.WorkPlan;
import cn.crm.dao.WorkPlanDao;

/**
 * @author Vi1___000
 *
 */
@Repository("workPlanDao")
public class WorkPlanDaoImpl extends HibernateDaoSupport implements WorkPlanDao {

	/* (non-Javadoc)
	 * @see cn.crm.dao.WorkPlanDao#getAll(cn.crm.bean.WorkPlan)
	 */
	@Override
	public List<WorkPlan> getAll(WorkPlan plan) {
	
		DetachedCriteria criteria = DetachedCriteria.forClass(WorkPlan.class);
		
		if(plan.getDDLByTypeCompletion()!=null){
			criteria.add(Restrictions.eq("DDLByTypeCompletion", plan.getDDLByTypeCompletion()));
		}
		
		if(plan.getDDLByTypeImportance()!=null){
			criteria.add(Restrictions.eq("DDLByTypeImportance", plan.getDDLByTypeImportance()));
		}
		
		if(plan.getUserByUsSuperintendent()!=null){
			criteria.add(Restrictions.eq("userByUsSuperintendent", plan.getUserByUsSuperintendent()));
		}
		
		if(plan.getWpMessagereminded()!=null){
			criteria.add(Restrictions.eq("wpMessagereminded", plan.getWpMessagereminded()));
		}
		
		if(plan.getWpIsdelete()!=null){
			criteria.add(Restrictions.eq("wpIsdelete", plan.getWpIsdelete()));
		}
		
		
		
		
		return super.getHibernateTemplate().findByCriteria(criteria);
	}

	/* (non-Javadoc)
	 * @see cn.crm.dao.WorkPlanDao#addWorkPlan(cn.crm.bean.WorkPlan)
	 */
	@Override
	public boolean addWorkPlan(WorkPlan plan) {
		// TODO Auto-generated method stub
		
		try {
			super.getHibernateTemplate().save(plan);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	/* (non-Javadoc)
	 * @see cn.crm.dao.WorkPlanDao#updateWorkPlan(cn.crm.bean.WorkPlan)
	 */
	@Override
	public boolean updateWorkPlan(WorkPlan plan) {
		// TODO Auto-generated method stub
		try {
			WorkPlan temp = this.getByID(plan.getWpPlanid());
			if(temp!=null){
				temp.setWpIsdelete(plan.getWpIsdelete());
				temp.setDDLByTypeCompletion(plan.getDDLByTypeCompletion());
				temp.setDDLByTypeImportance(plan.getDDLByTypeImportance());
				super.getHibernateTemplate().update(temp);
			}
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	@Override
	public WorkPlan getByID(int id) {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate().get(WorkPlan.class, id);
	}

	@Override
	public List<WorkPlan> getByUserAndTime(User user, Date start, Date end) {	
		DetachedCriteria criteria = DetachedCriteria.forClass(WorkPlan.class);
		criteria.add(Restrictions.eq("userByUsSuperintendent", user));
		
		if(start!=null && end!=null){
			criteria.add(Restrictions.between("wpRemindedtime", start, end));
		}else if(start!=null){
			criteria.add(Restrictions.le("wpRemindedtime", start));
		}else if(end!=null){
			criteria.add(Restrictions.ge("wpRemindedtime", end));
		}
		return super.getHibernateTemplate().findByCriteria(criteria);
	}

}
